{
  "cells": [
    {
      "attachments": {},
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "view-in-github"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/hsgw/keyboard-made-by-python/blob/main/notebook/en/what_is_colaboratory.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "> Project page is [Keyboard as a Python Code](https://hackaday.io/project/188907-keyboard-as-a-python-code)\n"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "metadata": {
        "id": "iBVgqeBuA9d9"
      },
      "source": [
        "# What is Google Colaboratory？\n",
        "\n",
        "Google Colaboratory is Python develop enviroment by Google.\n",
        "\n",
        "You can run python code without setting up an environment on your own computer and write document by markdown.\n",
        "\n",
        "In Google Colaboratory, an entire file is called a `notebook` and a chunk of code or documentation is called a `cell`, and if the `cell` type is code, clicking the `▷` icon will execute it.\n",
        "When execution is done, the result will be displayed below the cell.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "I952Ee82A9eA"
      },
      "outputs": [],
      "source": [
        "1 + 2\n"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "metadata": {
        "id": "kFJTKn2vA9eB"
      },
      "source": [
        "It keeps state until it exits, like a Python interpreter, so it keeps libraries and variables imported from one cell to another. Therefore, the order of execution may be limited. Most of the time, the order of execution is from the top to the bottom."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "zQ-WBWT1A9eC"
      },
      "outputs": [],
      "source": [
        "num = 10\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "1tpeXhRsA9eC"
      },
      "outputs": [],
      "source": [
        "print(num)\n",
        "num\n"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "metadata": {
        "id": "KBtF5rEoA9eC"
      },
      "source": [
        "You can also access bash in a virtual environment running Python.  \n",
        "This can be used to install libraries via pip, change the version of python itself, or clone a project from github.\n",
        "\n",
        "Colaboratory already has some commonly used libraries installed. Let's check them as well.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "prpb-nLaA9eC"
      },
      "outputs": [],
      "source": [
        "# If you start a line with \"!\", it becomes a command to bash\n",
        "!python --version\n",
        "!pip list"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "metadata": {
        "id": "OtUUjHcNA9eD"
      },
      "source": [
        "# Let's try it out\n",
        "\n",
        "Let's quickly run a script to generate and save a simple image in python.\n",
        "\n",
        "Import the image processing library pillow.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "I0LWySJvA9eD"
      },
      "outputs": [],
      "source": [
        "from PIL import Image, ImageDraw\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "t9SUodyRA9eE"
      },
      "outputs": [],
      "source": [
        "# Generate an image file with a rectangle\n",
        "size = (300, 200)\n",
        "img = Image.new(\"RGB\", size, (255, 255, 255))\n",
        "draw = ImageDraw.Draw(img)\n",
        "draw.rectangle((20, 20, 150, 150), fill=(255, 0, 0))\n",
        "\n",
        "# Display the generated image\n",
        "img"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "J22ChRcCA9eE"
      },
      "outputs": [],
      "source": [
        "# Add a circle\n",
        "draw.ellipse((50, 50, 200, 200), fill=(0, 255, 0))\n",
        "img"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "JrTzWf-2A9eE"
      },
      "outputs": [],
      "source": [
        "# save the file\n",
        "img.save(\"test.png\")\n"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "metadata": {
        "id": "OBHr1gy1A9eE"
      },
      "source": [
        "If you saved a file, it may disappear once you close the notebook because the environment is reset. Download the file beforehand.\n",
        "\n",
        "Saved files are shown in the file browser on the left side tab."
      ]
    }
  ],
  "metadata": {
    "colab": {
      "include_colab_link": true,
      "provenance": []
    },
    "kernelspec": {
      "display_name": ".venv",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.8.10 (default, Jun 22 2022, 20:18:18) \n[GCC 9.4.0]"
    },
    "orig_nbformat": 4,
    "vscode": {
      "interpreter": {
        "hash": "b68c839e49670d3aa77ea8bdd6f541fdcce81585b85d6e1d97674ad5f32db92f"
      }
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
